﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;

namespace QuanLyNhaHang.Bussiness
{
    class BusOrder
    {
        DataAccess.DAOrder dataOrder;
        GUI.Order f;

        public BusOrder(GUI.Order f2)
        {
            f = f2;
            dataOrder = new DataAccess.DAOrder();
        }

        public void loadListTable()
        {
            f.listViewTable.Clear();
            f.listViewTable.Groups.Clear();

            f.listViewTable.ShowGroups = true;
            f.listViewTable.View = View.LargeIcon;
            f.listViewTable.LargeImageList = f.imageList1;

            DataTable dsKhuVuc = dataOrder.loadListView("dsKhuVuc");
            DataTable dsBan = dataOrder.loadListView("dsBan");

            String[] arrKhuVuc = new String[dsKhuVuc.Rows.Count];

            int i = 0;
            foreach (DataRow row in dsKhuVuc.Rows)
            {
                arrKhuVuc[i++] = row[0].ToString();
                f.listViewTable.Groups.Add(new System.Windows.Forms.ListViewGroup(row[0].ToString()));
            }


            
            foreach (DataRow row in dsBan.Rows)
            {
                for (int j = 0; j < arrKhuVuc.Length; j++)
                {
                    if (arrKhuVuc[j] == row[1].ToString())
                    {
                        ListViewItem item = new ListViewItem(row[0].ToString());
                        item.ImageIndex = 0;
                        item.Group = f.listViewTable.Groups[j];
                        f.listViewTable.Items.Add(item);
                    }
                }
            }
            
        }

        public void loadListTDon()
        {
            this.f.listViewTDon.Clear();
            this.f.listViewTDon.Groups.Clear();

            ColumnHeader cl1 = new ColumnHeader();
            ColumnHeader cl2 = new ColumnHeader();
            ColumnHeader cl3 = new ColumnHeader();
            ColumnHeader cl4 = new ColumnHeader();

            cl1.Text = "Tên thực đơn"; cl1.Width = 99;
            cl2.Text = "Mã thực đơn"; cl2.Width = 81;
            cl3.Text = "ĐVT"; cl3.Width = 65; //81
            cl4.Text = "Đơn giá"; cl4.Width = 81;

            this.f.listViewTDon.Columns.Add(cl1);
            this.f.listViewTDon.Columns.Add(cl2);
            this.f.listViewTDon.Columns.Add(cl3);
            this.f.listViewTDon.Columns.Add(cl4);

            this.f.listViewTDon.ShowGroups = true;
            DataTable dsDMTD = dataOrder.loadListView("dsDMTD");
            DataTable dsTD = dataOrder.loadListView("dsTD");

            String[] arrDMTD = new String[dsDMTD.Rows.Count];

            int i = 0;
            foreach (DataRow row in dsDMTD.Rows)
            {
                arrDMTD[i++] = row[0].ToString();
                f.listViewTDon.Groups.Add(new System.Windows.Forms.ListViewGroup(row[0].ToString()));
            }


            foreach (DataRow row in dsTD.Rows)
            {
                for (int j = 0; j < arrDMTD.Length; j++)
                {
                    if (arrDMTD[j] == row[2].ToString())
                    {
                        ListViewItem item = new ListViewItem(row[1].ToString());

                        ListViewItem.ListViewSubItem subMa = new ListViewItem.ListViewSubItem();
                        subMa.Text = row[0].ToString();
                        item.SubItems.Add(subMa);

                        ListViewItem.ListViewSubItem subDVT = new ListViewItem.ListViewSubItem();
                        subDVT.Text = row[3].ToString();
                        item.SubItems.Add(subDVT);

                        ListViewItem.ListViewSubItem subDG = new ListViewItem.ListViewSubItem();
                        subDG.Text = row[4].ToString();
                        item.SubItems.Add(subDG);

                        item.Group = f.listViewTDon.Groups[j];
                        f.listViewTDon.Items.Add(item);
                    }
                }
            }

            //this.f.listViewTDon.Groups.Add(new ListViewGroup("Hello"));

            //ListViewItem item = new ListViewItem("Hahaha");
            //ListViewItem.ListViewSubItem subMa = new ListViewItem.ListViewSubItem();
            //subMa.Text = "Ma01";
           // item.SubItems.Add(subDG);

            //item.Group = f.listViewTDon.Groups[0];
            //f.listViewTDon.Items.Add(item);

        }

        public String getLocationForNewTable()
        {
            String s = this.f.listViewTable.SelectedItems[0].Text.ToString();

            String a = dataOrder.getLocationForNewTable(s);
            return a;
        }

        public void deleteTable(String table)
        {
            DialogResult result = MessageBox.Show("Bạn có muốn xóa bàn này!!!!", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            // hỏi có đồng ý không, nếu yes thì...
            if (result == DialogResult.Yes)
            {
                if(dataOrder.deleteTable(table)){
                    loadListTable();
                }
            }
        }
    }
}
